home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1444 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: news.cs.ucla.edu!edwin
  2. From: edwin@cs.ucla.edu (E. Robert Tisdale)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Overloading >>
  5. Date: 11 Jan 1996 02:43:42 GMT
  6. Organization: UCLA Computer Science Dept.
  7. Message-ID: <4d1tgu$5r0@delphi.cs.ucla.edu>
  8. References: <4d1f4q$7oj@news.cs.hope.edu>
  9. NNTP-Posting-Host: flamingo.cs.ucla.edu
  10. X-Newsreader: NN version 6.5.0.b3.0 #9 (NOV)
  11.  
  12. vnopstal@cs.hope.edu (Michael Van Opstall) writes:
  13.  
  14. >I was actually pretty clear on output stream overloading, but I really have
  15. >a question on overloading the >> operator. I really just need the function
  16. >header and param list. Right now, I'm using this:
  17.  
  18. >istream& operator>>(istream& in, typename t1)
  19.  
  20. >which compiles, but I don't understand what stream is which, nor what to
  21. >do with t1. Am I supposed to do something like
  22. >in >> whatever >> I >> want;
  23. >t1.fpart=whatever;
  24. >t1.mpart=I;
  25. >t1.lpart=want;
  26. >return in;
  27.  
  28. >or what? Why do I need to return an istream&? Same reason the = op requires
  29. >a return?
  30.  
  31. istream& operator>>(istream& in, typename& t1) {
  32.   type_fpart        whatever;
  33.   if (in >> whatever) {
  34.     type_mpart        I;
  35.     if (in >> I) {
  36.       type_lpart    want;
  37.       if (in >> want) {
  38.     t1 = typename(whatever, I, want);
  39.         }
  40.       else {
  41.         cerr << "Error reading lpart!\n";
  42.         }
  43.       }
  44.     else {
  45.       cerr << "Error reading mpart!\n";
  46.       }
  47.     }
  48.   else {
  49.     cerr << "Error reading fpart!\n";
  50.     }
  51.   return in;
  52.   }
  53.  
  54. 1.)    Pass a reference to your input variable t1.
  55. 2.)    Don't alter t1 before you have read all the parts.
  56. 3.)    Return in to stop the compiler from complaining about it.
  57.     It doesn't really matter because in was passed by reference.
  58.  
  59. Hope this helps, Bob Tisdale.
  60.